#include "Threshold.h"
#include "../utils/Logger.h"

Threshold::Threshold(int threshold, int maxValue, int thresholdType) :
        threshold(threshold),
        maxValue(maxValue),
        thresholdType(thresholdType)
{
}

Threshold::~Threshold() {
}

IplImage* Threshold::apply(const IplImage* in) {
    Log << "Applying threshold effect: " <<
            threshold << ", " <<
            maxValue << ", " <<
            thresholdType << endl;

    bool releaseIn = false;
    if (in->nChannels != 1) {
        in = grayEffect.apply(in);
        releaseIn = true;
    }

    IplImage *out = cvCreateImage(cvSize(in->width, in->height), in->depth, in->nChannels);
    cvThreshold(in, out, threshold, maxValue, thresholdType);

    if (releaseIn)
        cvReleaseImage((IplImage**) &in);

    return out;
}
